using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using DAL;
using BUS;
using Utility;

public partial class admin_Chitietsanpham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
           
            int id = int.Parse(Request.QueryString["id"]);
            tbSanPham sp = tbSanPhamBUS.GetByID(id);
            
         
            lit_ma.Text = sp.MaSP.ToString();


            hpl_them.NavigateUrl = "~/admin/Themhinhanh.aspx?id=" + sp.MaSP .ToString();

            lit_tensp.Text = sp.TenSP;

            tbLoaiMH loaimh = tbLoaiMHBUS.GetByID(sp.MaLoai );
            lit_loaimh.Text = loaimh.Ten ;            

            tbDoanhNghiep dn = tbDoanhNghiepBUS.GetByID(sp.MaDN );
            lit_dn.Text = dn.TenDN ;

            lit_giagoc.Text = sp.GiaGoc .ToString();
            lit_giaban.Text = sp.GiaBan .ToString();
            lit_soluongphieu.Text = sp.SLphieu .ToString();
            lit_soluongpb.Text = sp.SLphieuban .ToString();           
            lit_chietkhau.Text = sp.ChietKhau .ToString();
            lit_thoigianbd.Text = sp.ThoiGianBD .ToString();
            lit_thoigiankt.Text = sp.ThoiGianKT .ToString();
            lit_mota.Text = sp.ChiTietSP ;

            tbTrangThai tt = tbTrangThaiBUS.GetByID(sp.TrangThai );
            lit_tinhtrang.Text = tt.Ten ;

            lit_dieukien.Text = sp.DieuKien ;


            img_HinhMinhHoa.ImageUrl =
                "~/Photos/Thumb/" + sp.Hinhminhhoa ;

            Pager.PageSize = 5;
            Criteria c = new Criteria();
            c.add(Condition.AND, tbSanPhamColumns.MaSP , Condition.EQUAL, id);
            int total = 0;
            tbHinhSanPhamCollection hinhspCollection =
                tbHinhSanPhamBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
                tbHinhSanPhamColumns.Masp .ToString(), OrderDirection.ASC.ToString(), out total);
            Repeater1.DataSource = hinhspCollection;
            Repeater1.DataBind();
            Pager.ItemCount = total;
        }
        catch
        {
        }

    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1 .Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbHinhSanPham ha = tbHinhSanPhamBUS.GetByID(id);
                if (ha.Tenhinh != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + ha.Tenhinh));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + ha.Tenhinh));
                }
                tbHinhSanPhamBUS.Delete(id);
            }
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
          e.Item.ItemType == ListItemType.Item)
        {
            tbHinhSanPham hinhsp = (tbHinhSanPham)e.Item.DataItem;
            //Literal lit_duongdan = (Literal)e.Item.FindControl("lit_duongdan");
            Image img_hinhanh = (Image)e.Item.FindControl("img_hinhanh");
            //lit_duongdan.Text = hinhsp.Duongdan ;
            img_hinhanh.ImageUrl = "~/Photos/Thumb/" + hinhsp.Tenhinh ;

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/admin/Suahinhanh.aspx?id=" + hinhsp.MaHinh .ToString();


        }
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}
